package top.yangbuyi.product.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yangbuyi.product.domain.DropDownBox;
import top.yangbuyi.product.domain.ProductFile;
import top.yangbuyi.product.mapper.ProductFileMapper;
import top.yangbuyi.product.service.DropDownBoxService;
import top.yangbuyi.product.service.ProductFileService;
import top.yangbuyi.product.vo.ProductFileVo;
import top.yangbuyi.system.common.ActiveUser;
import top.yangbuyi.system.common.Constant;
import top.yangbuyi.system.common.DataGridView;
import top.yangbuyi.system.common.StringUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassName: ProductFileServiceImpl
 * @Author: campsis-tk
 * @Date: 2020/7/15 21:53
 * @Description: 产品档案登记服务层
 **/
@Slf4j
@Service
public class ProductFileServiceImpl extends ServiceImpl<ProductFileMapper, ProductFile> implements ProductFileService {

	@Autowired
	private ProductFileMapper productFileMapper;

	@Autowired
	private DropDownBoxService dropDownBoxService;


	@Override
	/**
	 * @Description: 查询产品档案登记(删除状态为C001-2)
	 * @param productFileVo
	 * @return: top.yangbuyi.system.common.DataGridView
	 * @Author: campsis-tk
	 * @Date: 2020/7/17
	 */
	public DataGridView queryAllProductFileDelete(ProductFileVo productFileVo) {
		IPage<ProductFile> page = new Page<>(productFileVo.getPage(), productFileVo.getLimit());
		QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
		wrapper.eq("delete_tag", Constant.DELETE_C001_2);
		wrapper.like(StringUtils.isNotBlank(productFileVo.getProductName()), "product_name", productFileVo.getProductName());
		wrapper.eq(StringUtils.isNotBlank(productFileVo.getCheckTag()), "check_tag", productFileVo.getCheckTag());
		wrapper.ge(productFileVo.getStartTime() != null, "register_time", productFileVo.getStartTime());
		wrapper.le(productFileVo.getEndTime() != null, "register_time", productFileVo.getEndTime());
		wrapper.orderByDesc("register_time");
		this.productFileMapper.selectPage(page, wrapper);
		List<ProductFile> records = page.getRecords();
		for (ProductFile record : records) {
			if (record.getCheckTag() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryTextDescribeByCheckTag(record.getCheckTag());
				record.setAuditStatus(dropDownBox.getTextDescribe());
			}
			if (record.getType() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByType(record.getType());
				record.setTheType(dropDownBox.getTextDescribe());
			}
			if (record.getProductClass() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByLevel(record.getProductClass());
				record.setLevel(dropDownBox.getTextDescribe());
			}
		}
		return new DataGridView(page.getTotal(), records);
	}


	@Override
	/**
	 * @Description: 查询产品档案登记(审核状态为S001-1)
	 * @param productFileVo
	 * @return: top.yangbuyi.system.common.DataGridView
	 * @Author: campsis-tk
	 * @Date: 2020/7/17
	 */
	public DataGridView queryAllProductFileByCheckTag(ProductFileVo productFileVo) {
		IPage<ProductFile> page = new Page<>(productFileVo.getPage(), productFileVo.getLimit());
		QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
		wrapper.eq( "delete_tag", Constant.DELETE_C001_1);//删除状态
		wrapper.eq( "check_tag", Constant.UNREVIEWED);//等待审核
		wrapper.like(StringUtils.isNotBlank(productFileVo.getProductName()), "product_name", productFileVo.getProductName());
		wrapper.ge(productFileVo.getStartTime() != null, "register_time", productFileVo.getStartTime());
		wrapper.le(productFileVo.getEndTime() != null, "register_time", productFileVo.getEndTime());
		wrapper.orderByDesc("register_time");
		this.productFileMapper.selectPage(page, wrapper);
		List<ProductFile> records = page.getRecords();
		for (ProductFile record : records) {
			if (record.getCheckTag() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryTextDescribeByCheckTag(record.getCheckTag());
				record.setAuditStatus(dropDownBox.getTextDescribe());
			}
			if (record.getType() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByType(record.getType());
				record.setTheType(dropDownBox.getTextDescribe());
			}
			if (record.getProductClass() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByLevel(record.getProductClass());
				record.setLevel(dropDownBox.getTextDescribe());
			}
		}
		return new DataGridView(page.getTotal(), records);
	}

	@Override
	/**
	 * @Description: 查询产品档案登记(审核状态为S001-2)
	 * @param productFileVo
	 * @return: top.yangbuyi.system.common.DataGridView
	 * @Author: campsis-tk
	 * @Date: 2020/7/17
	 */
	public DataGridView queryAllProductFileByCheckTagS2(ProductFileVo productFileVo) {
		IPage<ProductFile> page = new Page<>(productFileVo.getPage(), productFileVo.getLimit());
		QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
		wrapper.eq( "delete_tag", Constant.DELETE_C001_1);//删除状态
		wrapper.eq( "check_tag", Constant.APPROVE_YES);//审核通过
		wrapper.like(StringUtils.isNotBlank(productFileVo.getProductName()), "product_name", productFileVo.getProductName());
		wrapper.ge(productFileVo.getStartTime() != null, "register_time", productFileVo.getStartTime());
		wrapper.le(productFileVo.getEndTime() != null, "register_time", productFileVo.getEndTime());
		wrapper.orderByDesc("register_time");
		this.productFileMapper.selectPage(page, wrapper);
		List<ProductFile> records = page.getRecords();
		for (ProductFile record : records) {
			if (record.getCheckTag() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryTextDescribeByCheckTag(record.getCheckTag());
				record.setAuditStatus(dropDownBox.getTextDescribe());
			}
			if (record.getType() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByType(record.getType());
				record.setTheType(dropDownBox.getTextDescribe());
			}
			if (record.getProductClass() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByLevel(record.getProductClass());
				record.setLevel(dropDownBox.getTextDescribe());
			}
		}
		return new DataGridView(page.getTotal(), records);
	}

	@Override
	/**
	 * @Description: 查询产品档案登记(删除状态为C001-1)
	 * @param productFileVo
	 * @return: top.yangbuyi.system.common.DataGridView
	 * @Author: campsis-tk
	 * @Date: 2020/7/17
	 */
	public DataGridView queryAllProductFileByDeleteTag(ProductFileVo productFileVo) {
		IPage<ProductFile> page = new Page<>(productFileVo.getPage(), productFileVo.getLimit());
		QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
		wrapper.eq( "delete_tag", Constant.DELETE_C001_1);
		wrapper.like(StringUtils.isNotBlank(productFileVo.getProductName()), "product_name", productFileVo.getProductName());
		wrapper.eq(StringUtils.isNotBlank(productFileVo.getCheckTag()), "check_tag", productFileVo.getCheckTag());
		wrapper.ge(productFileVo.getStartTime() != null, "register_time", productFileVo.getStartTime());
		wrapper.le(productFileVo.getEndTime() != null, "register_time", productFileVo.getEndTime());
		wrapper.orderByDesc("register_time");
		this.productFileMapper.selectPage(page, wrapper);
		List<ProductFile> records = page.getRecords();
		for (ProductFile record : records) {
			if (record.getCheckTag() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryTextDescribeByCheckTag(record.getCheckTag());
				record.setAuditStatus(dropDownBox.getTextDescribe());
			}
			if (record.getType() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByType(record.getType());
				record.setTheType(dropDownBox.getTextDescribe());
			}
			if (record.getProductClass() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByLevel(record.getProductClass());
				record.setLevel(dropDownBox.getTextDescribe());
			}
		}
		return new DataGridView(page.getTotal(), records);
	}

	@Override
	/**
	 * @param productFileVo
	 * @Description: 查询产品档案登记(类型为商品的数据)
	 * @return: java.lang.Object
	 * @Author: campsis-tk
	 * @Date: 2020/7/15
	 */
	public DataGridView queryAllProductFileByGoods(ProductFileVo productFileVo) {
		IPage<ProductFile> page = new Page<>(productFileVo.getPage(), productFileVo.getLimit());
		QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
		wrapper.eq("type", Constant.TYPE_Y001_1);//类型为商品
		wrapper.eq( "delete_tag", Constant.DELETE_C001_1);//删除状态
		wrapper.eq( "check_tag", Constant.APPROVE_YES);//审核通过
		wrapper.like(StringUtils.isNotBlank(productFileVo.getProductName()), "product_name", productFileVo.getProductName());
		wrapper.ge(productFileVo.getStartTime() != null, "register_time", productFileVo.getStartTime());
		wrapper.le(productFileVo.getEndTime() != null, "register_time", productFileVo.getEndTime());
		wrapper.orderByDesc("register_time");
		this.productFileMapper.selectPage(page, wrapper);
		List<ProductFile> records = page.getRecords();
		for (ProductFile record : records) {
			if (record.getCheckTag() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryTextDescribeByCheckTag(record.getCheckTag());
				record.setAuditStatus(dropDownBox.getTextDescribe());
			}
			if (record.getType() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByType(record.getType());
				record.setTheType(dropDownBox.getTextDescribe());
			}
			if (record.getProductClass() != null) {
				DropDownBox dropDownBox = this.dropDownBoxService.queryByLevel(record.getProductClass());
				record.setLevel(dropDownBox.getTextDescribe());
			}
		}
		return new DataGridView(page.getTotal(), records);
	}


	@Override
	public ProductFile saveProductFile(ProductFile productFile) {
		Random random = new Random();
		//设置产品档案的流水号
		int number = random.nextInt(9999999) + 1;
		log.info("产品档案的流水号: " + number);
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
		productFile.setRegister(activeUser.getUser().getName());    //设置设计人
		productFile.setProductId("100" + productFile.getFirstKindId() + "" + productFile.getSecondKindId() + "" + productFile.getThirdKindId() + "" + number);
		productFile.setRegisterTime(new Date());
		productFile.setCheckTag("S001-1");           //审核状态 默认等待审核
		productFile.setChangeTag("D002-1");          //
		productFile.setPriceChangeTag("J001-1");     //
		productFile.setDeleteTag("C001-1");          //删除状态  默认不删除
		productFile.setDesignCellTag("K001-1");      //
		productFile.setDesignModuleTag("W001-1");    //
		productFile.setDesignProcedureTag("G001-1"); //
		log.info("ProductFile: " + productFile.toString());
		this.productFileMapper.insert(productFile);
		return productFile;
	}

	@Override
	public ProductFile updateProductFile(ProductFile productFile) {
		productFile.setCheckTag("S001-1"); //修改产品档案之后需要再次审核
		this.productFileMapper.updateById(productFile);
		return productFile;
	}

	@Override
	public ProductFile updateDeleteProductFile(Integer id) {
		ProductFile productFile = this.productFileMapper.selectById(id);
		//修改删除状态
		productFile.setDeleteTag("C001-2");
		this.productFileMapper.updateById(productFile);
		return productFile;
	}

	@Override
	public ProductFile updateCheckYesProductFile(Integer[] ids) {
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
		if (ids != null) {
			ProductFile productFile = null;
			for (Integer id : ids) {
				productFile = this.productFileMapper.selectById(id);
				//修改审核状态  通过
				productFile.setCheckTag("S001-2");
				productFile.setChanger(activeUser.getUser().getName());    //设置审核人
				productFile.setCheckTime(new DateTime());           //设置审核时间
				this.productFileMapper.updateById(productFile);
			}
			return productFile;
		} else {
			return null;
		}
	}

	@Override
	public ProductFile updateCheckNoProductFile(Integer[] ids) {
		ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
		if (ids != null) {
			ProductFile productFile = null;
			for (Integer id : ids) {
				productFile = this.productFileMapper.selectById(id);
				//修改审核状态  不通过
				productFile.setCheckTag("S001-3");
				productFile.setChanger(activeUser.getUser().getName());    //设置审核人
				productFile.setCheckTime(new DateTime());           //设置审核时间
				this.productFileMapper.updateById(productFile);
			}
			return productFile;
		} else {
			return null;
		}
	}

	@Override
	public ProductFile updateDeleteBackProductFile(Integer[] ids) {
		ProductFile productFile = null;
		if (ids != null) {
			for (Integer id : ids) {
				productFile = this.productFileMapper.selectById(id);
				//修改删除状态
				productFile.setDeleteTag("C001-1");
				this.productFileMapper.updateById(productFile);
			}
			return productFile;
		} else {
			return null;
		}
	}

	@Override
	public void batchDeleteProductFile(Integer[] ids) {
		if (ids != null) {
			for (Integer id : ids) {
				this.productFileMapper.deleteById(id);
			}

		}
	}

	@Override
	public ProductFile getById(Serializable id) {
		return super.getById(id);
	}
}
